<template>
  <div class="user">
    <div>
      <el-form
        :inline="true"
        label-width="100px"
        style="padding-top: 20px; background-color: #fff"
      >
        <el-form-item label="用户名称">
          <el-input
            v-model="userPagination.name"
            autocomplete="off"
            placeholder="请输入"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="small" @click="handleSearch"
            >搜索</el-button
          >
          <el-button
            type="warning"
            size="small"
            @click="dialogFormVisibleAddUser = true"
            >新增</el-button
          >
        </el-form-item>
      </el-form>
    </div>
    <xyc-table
      :table-data="userListData.data"
      :list-data="userConfig"
      :page="{ total: userListData.total }"
    >
      <template #opeartion="scope">
        <el-button type="primary" size="mini" @click="handleEdit(scope.row)"
          >编辑</el-button
        >
        <el-button type="danger" size="mini" @click="handleDel(scope.row)"
          >删除</el-button
        >
      </template>
    </xyc-table>
    <el-dialog title="编辑角色" :visible.sync="dialogFormVisibleEditUser">
      <el-form :model="editUserInfo" label-width="100px">
        <el-form-item label="密码">
          <el-input
            v-model="editUserInfo.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="editUserInfo.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input
            v-model="editUserInfo.true_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色Id">
          <el-select v-model="editUserInfo.jobs_id">
            <el-option
              v-for="item in $store.state.roles"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启">
          <el-select v-model="editUserInfo.status">
            <el-option :value="0" label="开启"></el-option>
            <el-option :value="1" label="关闭"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleEditUser = false">取 消</el-button>
        <el-button type="primary" @click="handleSave(2)">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增角色" :visible.sync="dialogFormVisibleAddUser">
      <el-form :model="addUserInfo" label-width="100px">
        <el-form-item label="用户名">
          <el-input
            v-model="addUserInfo.user_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="addUserInfo.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="addUserInfo.mobile" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名">
          <el-input
            v-model="addUserInfo.true_name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="角色Id">
          <el-select v-model="addUserInfo.jobs_id">
            <el-option
              v-for="item in $store.state.roles"
              :key="item.id"
              :value="item.id"
              :label="item.name"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="是否开启">
          <el-select v-model="addUserInfo.status">
            <el-option :value="0" label="开启"></el-option>
            <el-option :value="1" label="关闭"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAddUser = false">取 消</el-button>
        <el-button type="primary" @click="handleSave(1)">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import xycTable from "@/ui/table/table";
import userConfig from "./config/userConfig";
import { userLists, userDel, userEdit, userAdd } from "@/api/user";
export default {
  components: { xycTable },
  data() {
    return {
      userConfig,
      userPagination: {
        page: 1,
        limit: 10,
        name: "",
      },
      userListData: {
        data: [],
      },
      dialogFormVisibleEditUser: false,
      editUserInfo: {
        admin_id: "",
        password: "",
        mobile: "",
        true_name: "",
        jobs_id: "",
        status: "",
      },
      dialogFormVisibleAddUser: false,
      addUserInfo: {
        user_name: "",
        password: "",
        mobile: "",
        true_name: "",
        jobs_id: "",
        status: "",
      },
    };
  },
  created() {
    this.getUserList();
  },
  methods: {
    async getUserList() {
      const userList = await userLists(this.userPagination);
      if (userList.code === 1) {
        this.userListData = userList;
      }
    },
    handleDel({ admin_id }) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const res = await userDel({ admin_id });
          if (res.code == 1) {
            this.$message({ type: "success", message: "删除成功!" });
            this.getUserList();
          } else {
            this.$message({ type: "error", message: res.msg });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleEdit(row) {
      this.editUserInfo.admin_id = row.admin_id;
      this.editUserInfo.password = row.password;
      this.editUserInfo.mobile = row.mobile;
      this.editUserInfo.true_name = row.true_name;
      this.editUserInfo.jobs_id = row.jobs_id;
      this.editUserInfo.status = row.status;
      this.dialogFormVisibleEditUser = true;
    },
    async handleSave(num) {
      if (num == 1) {
        await userAdd(this.addUserInfo).then((res) => {
          if (res.code == 1) {
            this.$message.success("新增成功!");
            this.dialogFormVisibleAddUser = false;
            this.getUserList();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        await userEdit(this.editUserInfo).then((res) => {
          if (res.code == 1) {
            this.$message.success("编辑成功!");
            this.dialogFormVisibleEditUser = false;
            this.getUserList();
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    handleSearch() {
      this.userPagination.page = 1;
      this.getUserList();
    },
  },
};
</script>

<style></style>
